module.exports = function ($scope, $modalInstance, $resource, $modal, item, flag, toaster){
    


    //获取父分类列表
    $scope.findtypeFlist = function (){
        console.log('type')
        $resource('/api/as/sc/dict/getDictInfoList', {}, {}).save({ 'type': 'sys_msgType' }, function(res){
            if(res.errcode === 0){
                $scope.dictbytypelist = res.data;
            }else{
                toaster.error({ title: "提示", body: res.errmsg });
            }
        });
    }


  
    init();
    function init(){
       
        $scope.info = flag ? {'flag': true} : angular.copy(item);
        $scope.info.company_codes = [];
        var aaa= $scope.info.send_companys;
        console.log(aaa)
        for(var i in aaa){
            console.log(aaa[i])
            $scope.info.company_codes.push(aaa[i]);
        }
        $scope.findtypeFlist();
        console.log($scope.info.company_codes)
    }


    

    //获取子分类列表
    $scope.findtypeSlist = function (){
        $resource('/api/as/sc/dict/getDictInfoList', {}, {})
			.save({ type: 'sys_msgType_'+$scope.info.msgType }, function (res) {
				if (res.errcode !== 0) {
					toaster.error({ title: "提示", body: res.errmsg });
					return;
				}
				$scope.dictbytypeSlist = res.data;
			});
    }





    //保存并发送
    $scope.save = function (){
        console.log($scope.info);
        var para = {
            'title': $scope.info.title,
            'info': $scope.info.info,
            'company_codes': $scope.info.company_codes,
            'companyType': $scope.info.companyType,
            'msgType': $scope.info.msgType,
            'msgTypeSon' : $scope.info.msgTypeSon
        }
        $resource('/api/ac/sc/informaticaService/save', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //保存
    $scope.saveonly = function (){
        console.log($scope.info);
        var para = {
            'title': $scope.info.title,
            'info': $scope.info.info,
            'company_codes': $scope.info.company_codes,
            'companyType': $scope.info.companyType,
            'msgType': $scope.info.msgType,
            'msgTypeSon' : $scope.info.msgTypeSon
        }
        $resource('/api/ac/sc/informaticaService/insertInformatica', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
   
    //添加机构
    $scope.add = function () {
        for(var i = 0; i < $scope.info.company_codes.length; i++){
            if($scope.info.company_code == $scope.info.company_codes[i].company_code){
                toaster.error({title: '', body: '不能重复添加'});
                return;
            }
        }
        var temp = {
            'company_code': $scope.info.company_code
        };
        $scope.info.company_code = '';
    $scope.info.company_codes.push(temp);
    }

    //删除已添加机构
    $scope.remove = function (index) {
        $scope.info.company_codes.splice(index,1)
    };
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }

};